<template>
  <div class="app-container">
    <el-form ref="formSearch" :model="searchForm" label-width="100px">
      <el-row>
        <el-col :span="19">
          <el-row class="form_table">
            <el-col :span="6">
              <el-form-item :label="$t('dictionaryItem.itemLabel')" prop="itemLabel">
                <el-input v-model.trim="searchForm.itemLabel" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('dictionaryItem.itemValue')" prop="itemValue">
                <el-input v-model.trim="searchForm.itemValue" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('dictionaryItem.remark')" prop="remark">
                <el-input v-model.trim="searchForm.remark" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="locale" :label="$t('dictionaryItem.locale')">
                <el-select v-model="searchForm.locale" clearable>
                  <el-option v-for="item in languageList" :key="item.id" :label="item.text" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="5" style="text-align: center">
          <el-button
            type="primary"
            @click="
              searchForm.pageNumber = 1
              getData()
            "
          >{{ $t('btn.query') }}</el-button>
          <el-button type="danger" @click="resetForm('formSearch')">{{ $t('btn.reset') }}</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-button v-permission="'gaeaDictItemController#insert'" type="primary" icon="el-icon-plus" @click="openCreateUser">{{ $t('btn.add') }}</el-button>
    <el-button v-permission="'gaeaDictItemController#update'" type="primary" icon="el-icon-edit" :disabled="selectedList.length != 1" @click="editDetail('edit', null)">{{ $t('btn.edit') }}</el-button>
    <delete-btn v-permission="'gaeaDictItemController#deleteBatchIds'" :disabled="selectedList.length < 1" @handleDelete="handleDelete" />
    <el-table stripe :data="tableList" border @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="50" center />
      <el-table-column prop="itemCode" :label="$t('dictionaryItem.itemCode')" min-width="110" align="center" />
      <el-table-column :label="$t('dictionaryItem.itemLabel')" min-width="110" align="center">
        <template slot-scope="scope">
          <span class="view" @click="editDetail('view', scope.row)">{{ scope.row.itemLabel }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="itemValue" :label="$t('dictionaryItem.itemValue')" min-width="110" align="center" />
      <el-table-column prop="remark" :label="$t('dictionaryItem.remark')" min-width="110" align="center" />
      <el-table-column prop="sort" :label="$t('dictionaryItem.sort')" min-width="110" align="center" />
      <el-table-column prop="localeView" :label="$t('dictionaryItem.locale')" min-width="110" align="center" />
      <el-table-column prop="createTime" :label="$t('businessGlossary.creationTime')" align="center" min-width="160" />
      <el-table-column prop="createByView" :label="$t('businessGlossary.creator')" align="center" min-width="160" />
      <el-table-column prop="updateTime" :label="$t('businessGlossary.modifyTime')" align="center" min-width="180" />
      <el-table-column prop="createByView" :label="$t('businessGlossary.modifyUser')" align="center" min-width="140" />
    </el-table>
    <el-pagination v-show="total > 0" background :current-page.sync="searchForm.pageNumber" :page-sizes="$pageSizeAll" :page-size="searchForm.pageSize" layout="total, prev, pager, next, jumper, sizes" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    <el-dialog :title="$t(`btn.${dialogTittle}`)" width="50%" :close-on-click-modal="false" center :visible.sync="basicDialog" @close="closeDialog">
      <el-form ref="userForm" :model="dialogForm" :rules="formRules" label-width="100px" :disabled="dialogTittle == 'view'">
        <el-row class="form_table">
           <el-col :span="12">
            <el-form-item :label="$t('dictionaryItem.itemCode')" prop="itemCode">
              <el-input v-model.trim="dialogForm.itemCode" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('dictionaryItem.itemLabel')" prop="itemLabel">
              <el-input v-model.trim="dialogForm.itemLabel" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('dictionaryItem.itemValue')" prop="itemValue">
              <el-input v-model.trim="dialogForm.itemValue" clearable :disabled="dialogTittle != 'add'" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('dictionaryItem.locale')" prop="locale">
              <el-select v-model="dialogForm.locale" clearable>
                <el-option v-for="item in languageList" :key="item.id" :label="item.text" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('dictionaryItem.sort')" prop="sort">
              <el-input v-model.trim="dialogForm.sort" clearable type="number" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item :label="$t('dictionaryItem.remark')" prop="remark">
              <el-input v-model.trim="dialogForm.remark" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('dictionaryItem.itemExtend')" prop="itemExtend">
              <el-input v-model.trim="dialogForm.itemExtend" type="textarea" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" style="text-align: center">
        <el-button v-if="dialogTittle != 'view'" type="primary" plain @click="confirm">{{ $t('btn.confirm') }}</el-button>
        <el-button type="danger" plain @click="basicDialog = false">{{ $t('btn.close') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getDictItemList, dictItemAdd, dictItemEdit, dictsItemDelect } from '@/api/system-set'
import { dataDictionary } from '@/api/common'
export default {
  name: 'DictItem',
  data() {
    return {
      languageList: [],
      selectedList: [],
      searchForm: {
        dictCode: this.$route.query.dictCode,
        itemCode: null,
        itemLabel: null,
        itemValue: null,
        remark: null,
        pageNumber: 1,
        pageSize: 10,
      },
      tableList: [],
      total: 0,
      dialogTittle: 'view',
      basicDialog: false,
      dialogForm: {
        dictCode: this.$route.query.dictCode,
        itemCode: null,
        itemLabel: null,
        remark: null,
        itemValue: null,
        locale: null,
        sort: null,
        itemExtend: null,
      },
      formRules: {
        locale: [{ required: true, message: this.$t('placeholder.select'), trigger: 'blur' }],
        itemValue: [{ required: true, message: this.$t('placeholder.input'), trigger: 'blur' }],
        itemCode: [{ required: true, message: this.$t('placeholder.input'), trigger: 'blur' }],
        itemLabel: [{ required: true, message: this.$t('placeholder.input'), trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getLang()
    this.getData()
  },
  methods: {
    async getLang() {
      const { code, data } = await dataDictionary('LOCALE')
      if (code != '200') return
      this.languageList = data
      console.log(this.languageList)
    },
    // 提交按钮
    confirm() {
      this.$refs.userForm.validate(async(valid, obj) => {
        if (valid) {
          if (this.dialogTittle == 'add') {
            const { code } = await dictItemAdd(this.dialogForm)
            if (code != '200') return
            this.closeDialog(true)
          } else {
            const { code } = await dictItemEdit(this.dialogForm)
            if (code != '200') return
            this.closeDialog(true)
          }
        } else {
          return
        }
      })
    },
    // 关闭弹窗
    closeDialog(bool) {
      bool && this.getData() // 点确定关闭弹窗的时候才会刷新列表
      this.$refs['userForm'].resetFields()
      this.basicDialog = false
    },
    // 删除操作
    async handleDelete() {
      const { code } = await dictsItemDelect(this.selectedList.map((item) => item.id))
      if (code != '200') return
      this.getData()
    },
    // 新建操作
    openCreateUser() {
      this.dialogTittle = 'add' // 新建
      this.basicDialog = true
    },
    // 编辑和查看操作
    editDetail(title, row) {
      this.dialogTittle = title
      this.basicDialog = true
      this.$nextTick(() => {
        this.dialogForm = JSON.parse(JSON.stringify(row || this.selectedList[0]))
      })
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.tableList = []
      this.total = 0
      this.getData()
    },
    // 查询
    async getData() {
      const { data, code } = await getDictItemList(this.searchForm)
      if (code != '200') return
      this.tableList = data.records
      this.total = data.total
    },
    // 选择项改变时
    handleSelectionChange(val) {
      this.selectedList = val
    },
    // 页码改变
    handleCurrentChange(pageNumber) {
      this.searchForm.pageNumber = pageNumber
      this.getData()
    },
    // 每页size改变时
    handleSizeChange(val) {
      this.searchForm.pageNumber = 1
      this.searchForm.pageSize = val
      this.getData()
    },
  },
}
</script>
